﻿#region --- License & Copyright Notice
/*
CRUD Services Library

Copyright 2012 Jeevan James

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#endregion

using System.Runtime.Serialization;
using System.Text;

namespace CrudServices
{
    [DataContract]
    public sealed class SearchCriteria
    {
        [DataMember(Name = "source")]
        private readonly string _source;

        [DataMember(Name = "sort")]
        private SortCriteria _sortFields = new SortCriteria();

        [DataMember(Name = "where")]
        private readonly WhereCriteria _whereCriteria = new WhereCriteria();

        internal SearchCriteria(string source)
        {
            _source = source;
        }

        public string Source
        {
            get { return _source; }
        }

        [DataMember(Name = "projection")]
        public string ProjectionTarget { get; set; }

        public SortCriteria SortFields
        {
            get { return _sortFields; }
        }

        public WhereCriteria WhereCriteria
        {
            get { return _whereCriteria; }
        }

        [DataMember(Name = "skip")]
        public int? Skip { get; set; }

        [DataMember(Name = "limit")]
        public int? Limit { get; set; }

        public override string ToString()
        {
            var sb = new StringBuilder();
            if (ProjectionTarget != null)
            {
                sb.Append("SELECT ");
                if (Skip.HasValue)
                    sb.Append("SKIP ").Append(Skip.Value).Append(" ");
                if (Limit.HasValue)
                    sb.Append("LIMIT ").Append(Limit.Value).Append(" ");
                sb.Append(ProjectionTarget).Append(" ");
                sb.AppendLine();
            }
            else
                sb.AppendLine("SELECT * ");
            sb.Append("FROM ").Append(_source).AppendLine();
            if (_whereCriteria.Count > 0)
            {
                sb.Append("WHERE ");
                for (int i = 0; i < _whereCriteria.Count; i++)
                {
                    WhereCriterion whereCriterion = _whereCriteria[i];
                    sb.AppendFormat("{0} {1} {2}", whereCriterion.FieldName, whereCriterion.Operand, whereCriterion.Value);
                    if (i < _whereCriteria.Count - 1)
                        sb.Append(" AND");
                    sb.AppendLine();
                }
            }
            if (_sortFields.Count > 0)
            {
                sb.Append("ORDER BY ");
                foreach (SortCriterion sortField in _sortFields)
                {
                    sb.Append(sortField.FieldName).Append(" ");
                    sb.Append(sortField.Order == SortOrder.Ascending ? "ASC" : "DESC");
                    sb.Append(", ");
                }
                sb.Remove(sb.Length - 2, 2);
                sb.AppendLine();
            }
            return sb.ToString();
        }

        [OnSerializing]
        private void BeforeSerializing(StreamingContext context)
        {
            if (_sortFields.Count == 0)
                _sortFields = null;
        }

        [OnDeserialized]
        private void AfterDeserializing(StreamingContext context)
        {
            if (_sortFields == null)
                _sortFields = new SortCriteria();
        }
    }
}